27. Exercise: LiveData Map Transformation

ANDK L5 50 LiveData Map Transformation SC V6

Now it’s your turn to complete this exercise yourself.

In this step you'll use a Tranformations.map to convert the current time into a formatted String.

1. In GameViewModel create a new LiveData called currentTimeString:

This will store the String version of currentTime.

2. Use Transformation.map to take currentTime to a String output from currentTimeString.

What you want is to use DateUtils to convert the currentTime number output into a String. Then we want to emit that from the currentTimeString LiveData. To do that:

val currentTimeString = Transformations.map(currentTime) { time ->
    DateUtils.formatElapsedTime(time)
}

3. Set timer_text to the value of currentTimeString:

In the game_fragment.xml, find the timer_text view and set the text attribute to the value of currentTimeString (not currentTime!) in a binding expression.

4. Delete the observer for currentTime from GameFragment:

We don't need it anymore! As always, run your code!

If you want to start at this step, you can download this exercise code from: Step.10-Exercise-Map-Transformation.

You will find plenty of //TODO comments to help you complete this exercise, and if you get stuck go back and watch the video again.

Once you’re done, you can check your solution against the solution we’ve provided here Step.10-Solution-Map-Transformation or using this git diff

Task Description:

Check the steps below as you implement them to complete this exercise.

Task List:

Task Feedback:

You've implemented a Map Transformation! You can check your solution against the solution we’ve provided here Step.10-Solution-Map-Transformation or using this git diff